package com.example.bookmanager.Controller;

import com.example.bookmanager.Model.UserInfo;
import com.example.bookmanager.Service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return "账号或密码为空";
        }
        UserInfo userInfo = userService.queryUserByName(username);
        if (userInfo == null) {
            return "查询不到该用户";
        }
        if (password.equals(userInfo.getPassword())) {
            session.setAttribute("username", userInfo);
            return "登陆成功";
        }
        return "账号密码不正确";
    }

}
